#include <termios.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <sys/ioctl.h>

static void pr_winsize(int fd){
	struct winsize size;

	if(ioctl(fd,TIOCGWINSZ,(char*)&size)<0){
		fprintf(stderr,"sz error\n");
		exit(-1);
	}
	printf("%d rows, %d cols\n",size.ws_row,size.ws_col);
}

static void sig_winch(int signo){
	printf("SIGWINCH received\n");
	pr_winsize(1);
}

int main(void){
	if(isatty(1)==0)
		exit(1);
	if(signal(SIGWINCH,sig_winch)==SIG_ERR){
		fprintf(stderr,"signal error\n");
		exit(-1);
	}
	pr_winsize(1);
	while(1)
		sleep(10000);
}
